package org.fh.controller.fhoa;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.fh.controller.base.BaseController;
import org.fh.dto.ClockinmyDTO;
import org.fh.entity.Page;
import org.fh.entity.PageData;
import org.fh.service.fhoa.ClockinmyService;
import org.fh.service.fhoa.ICustomerInfoService;
import org.fh.service.system.UsersService;
import org.fh.util.DateUtil;
import org.fh.util.Jurisdiction;
import org.fh.util.ObjectExcelView;
import org.fh.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 借款业务
 */

@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Autowired
    private ICustomerInfoService iCustomerInfoService;

    @Autowired
    private ClockinmyService clockinmyService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UsersService usersService;


    /**
     * 保存
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions("customer:add")
    @ResponseBody
    public Object add(
            @RequestParam(value = "CUSTOMER_NAME", required = false) String CUSTOMER_NAME,
            @RequestParam(value = "POSITION", required = false) String POSITION,
            @RequestParam(value = "COMPANY", required = false) String COMPANY,
            @RequestParam(value = "CUSTOMERCHANNELNAME", required = false) String CUSTOMERCHANNELNAME,
            @RequestParam(value = "CUSTOMERLEVEL", required = false) String CUSTOMERLEVEL,
            @RequestParam(value = "TEL", required = false) String TEL,
            @RequestParam(value = "ISCHANCE", required = false) String ISCHANCE,
            @RequestParam(value = "STAGE", required = false) String STAGE,
            @RequestParam(value = "REMARK", required = false) String REMARK

    ) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        //写入表
        pd.put("CUSTOMER_ID", this.get32UUID());
        pd.put("CUSTOMER_NAME", CUSTOMER_NAME);
        pd.put("POSITION", POSITION);
        pd.put("COMPANY", COMPANY);
        pd.put("CUSTOMERCHANNELNAME", CUSTOMERCHANNELNAME);
        pd.put("CUSTOMERLEVEL", CUSTOMERLEVEL);
        pd.put("TEL", TEL);
        pd.put("ISCHANCE", ISCHANCE);
        pd.put("STAGE", STAGE);
        pd.put("USERNAME", Jurisdiction.getUsername());


        PageData user = new PageData();
        user.put("USERNAME", Jurisdiction.getUsername());
        PageData Username = usersService.findByUsername(user);
        if (!Username.isEmpty()) {
            String USER_ID = Username.getString("USER_ID"); // 假设getString自动处理null
            System.out.println("USERID---------"+USER_ID);
            if (USER_ID == null) {

                pd.put("USER_ID", "用户名为获取");
            } else {
                pd.put("USER_ID", USER_ID);
            }
        }
        pd.put("CREATE_TIME", new Date());    //创建时间
        pd.put("UPDATE_TIME", new Date());    //创建时间
        List<ClockinmyDTO> PHOTOADDA = clockinmyService.getPhotoAddaByUnameAndCustomerName(Jurisdiction.getName(), CUSTOMER_NAME);
        System.out.println("PHOTOADDA----------"+PHOTOADDA);
        if (PHOTOADDA.isEmpty()) {
            pd.put("PHOTOADDA", "图片未获取到");

        } else {
            pd.put("PHOTOADDA", PHOTOADDA);

        }
        pd.put("REMARK", REMARK);
        iCustomerInfoService.save(pd);
        errInfo = "success";
//        }

        pd.put("result", errInfo);                //返回结果
        return pd;
    }


    /**
     * 删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("customer:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        iCustomerInfoService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }


    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("customer:edit")
    @ResponseBody
    public Object edit(
            @RequestParam(value = "CUSTOMER_NAME", required = false) String CUSTOMER_NAME,
            @RequestParam(value = "POSITION", required = false) String POSITION,
            @RequestParam(value = "COMPANY", required = false) String COMPANY,
            @RequestParam(value = "CUSTOMERCHANNELNAME", required = false) String CUSTOMERCHANNELNAME,
            @RequestParam(value = "CUSTOMERLEVEL", required = false) String CUSTOMERLEVEL,
            @RequestParam(value = "CONTACT_PERSON", required = false) String CONTACT_PERSON,
            @RequestParam(value = "TEL", required = false) String TEL,
            @RequestParam(value = "ISCHANCE", required = false) String ISCHANCE,
            @RequestParam(value = "INDUSTRY_ID", required = false) String INDUSTRY_ID,
            @RequestParam(value = "STAGE", required = false) String STAGE,
            @RequestParam(value = "REMARK", required = false) String REMARK
    ) throws Exception {
        String errInfo = "success";
        PageData pd = new PageData();


        pd.put("CUSTOMER_ID", this.get32UUID());
        pd.put("CUSTOMER_NAME", CUSTOMER_NAME);
        pd.put("POSITION", POSITION);
        pd.put("CUSTOMERCHANNELNAME", CUSTOMERCHANNELNAME);
        pd.put("COMPANY", COMPANY);
        pd.put("CONTACT_PERSON", CONTACT_PERSON);
        pd.put("CUSTOMERLEVEL", CUSTOMERLEVEL);
        pd.put("TEL", TEL);
        pd.put("ISCHANCE", ISCHANCE);
        pd.put("INDUSTRY_ID", INDUSTRY_ID);
        pd.put("STAGE", STAGE);
        pd.put("REMARK", REMARK);
        pd.put("PHOTOADDA", "");
        pd.put("CREATE_TIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("UPDATE_TIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("USERNAME", Jurisdiction.getUsername());    //
//		pd.put("UNAME", Jurisdiction.getName());
//		pd.put("USERNAME", Jurisdiction.getUsername());
//		pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
        iCustomerInfoService.edit(pd);
        pd.put("result", errInfo);                //返回结果
        return pd;
    }

    /**
     * 查询单个数据
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
    @RequiresPermissions("customer:edit")
    @ResponseBody
    public Object goEdit(@RequestParam(value = "CUSTOMER_ID", required = false) String CUSTOMER_ID) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CUSTOMER_ID", CUSTOMER_ID);
        pd = iCustomerInfoService.findById(pd);    //根据ID读取
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions("customer:list")
    @ResponseBody
    public Object list(Page page) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();

        String isMy = pd.getString("isMy");
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        String lastStart = pd.getString("lastStart");                    //开始时间
        String lastEnd = pd.getString("lastEnd");                        //结束时间
        if (Tools.notEmpty(lastStart)) pd.put("lastStart", lastStart + " 00:00:00");
        if (Tools.notEmpty(lastEnd)) pd.put("lastEnd", lastEnd + " 00:00:00");
        String USERNAME = Jurisdiction.getUsername();                    //用户名
        String item = Jurisdiction.getDEPARTMENT_IDS();
        if ("false".equals(isMy)) {
            if ("".equals(item) || "无权".equals(item)) {
                pd.put("item", "");                                            //根据部门ID过滤
                if (!"admin".equals(USERNAME)) pd.put("USERNAME", USERNAME);    //非admin用户时
            } else {
                pd.put("USERNAME", USERNAME);
                pd.put("item", item.replaceFirst("\\(", "\\('" + Jurisdiction.getDEPARTMENT_ID() + "',"));
            }
        } else {
            pd.put("USERNAME", USERNAME);
        }


        page.setPd(pd);
        List<PageData> varList = iCustomerInfoService.list(page);            //列出Worklog列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("USERNAME", USERNAME);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 导出到excel
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/excel")
    @RequiresPermissions("toExcel")
    public ModelAndView exportExcel() throws Exception {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("客户名称");    //1
        titles.add("客户岗位");    //2
        titles.add("单位");    //3
        titles.add("客户类型");    //4
        titles.add("客户等级");    //5
        titles.add("联系人姓名");    //6
        titles.add("联系人电话");    //7
        titles.add("商机");    //8
        titles.add("行业名称");    //11
        titles.add("跟进阶段");    //12
        titles.add("客户信息创建时间");    //13
        titles.add("商机客户信息更新时间");    //14
        titles.add("备注信息");    //15


        dataMap.put("titles", titles);
        List<PageData> varOList = iCustomerInfoService.listAll(pd);
        List<PageData> varList = new ArrayList<PageData>();
        for (int i = 0; i < varOList.size(); i++) {
            PageData vpd = new PageData();
            vpd.put("var1", varOList.get(i).getString("CUSTOMER_NAME"));        //1
            vpd.put("var2", varOList.get(i).getString("POSITION"));        //2
            vpd.put("var3", varOList.get(i).getString("COMPANY"));        //2
            vpd.put("var4", varOList.get(i).getString("CUSTOMERCHANNELNAME"));        //2
            vpd.put("var5", varOList.get(i).getString("CUSTOMERLEVEL"));        //2
            vpd.put("var6", varOList.get(i).getString("CONTACT_PERSON"));        //4
            vpd.put("var7", varOList.get(i).getString("TEL"));        //4
            vpd.put("var8", varOList.get(i).getString("ISCHANCE"));        //4
            vpd.put("var10", varOList.get(i).getString("INDUSTRY_ID"));        //4
            vpd.put("var11", varOList.get(i).getString("STAGE"));        //4
            vpd.put("var12", varOList.get(i).getString("USERNAME"));        //4
            vpd.put("var13", varOList.get(i).getString("CREATE_TIME"));        //4
            vpd.put("var14", varOList.get(i).getString("UPDATE_TIME"));        //4
            vpd.put("var15", varOList.get(i).getString("REMARK"));        //4

            varList.add(vpd);
        }
        dataMap.put("varList", varList);
        ObjectExcelView erv = new ObjectExcelView();
        mv = new ModelAndView(erv, dataMap);
        return mv;
    }
}
